//
//  HomeViewController.m
//  NekaPrice
//
//  Created by yu on 2018/6/9.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeViewModel.h"
#import "CheckLabel.h"
#import "HomeCell.h"
#import "ProductButton.h"
#import "PickerViewController.h"
#import "QuotationTopView.h"


static CGFloat kTopMargin = 350;
@interface HomeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)QuotationTopView *topView;
@property (nonatomic, strong)UIView *bottomView;
@property (nonatomic, strong)NSMutableArray *btnArray;
@property (nonatomic, strong)HomeViewModel *homeVM;
@property (nonatomic, strong)UITableView *tableView;

//项目大分类 0:改色贴膜, 1:隐形车衣 2:玻璃贴膜
@property (nonatomic, assign)NSInteger index;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.customBar showBackBtn:NO closeBtn:NO shareBtn:NO];
    [self hideNavBar];
    _index = 0;
    
    //背景图
    UIImageView *bgView = [[UIImageView alloc] init];
    bgView.image = [UIImage imageNamed:@"body"];
    [self.view addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);  //self.customBar.height
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-self.bottom);
    }];
    
    
    [self.view bringSubviewToFront:self.contentView];
    [self.contentView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-self.bottom);
    }];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(-kTopMargin);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.centerX.mas_equalTo(self.tableView.mas_centerX);
        make.height.mas_equalTo(kTopMargin);
    }];
    
    
    
    //创建VM
    _homeVM = [[HomeViewModel alloc] init];
    [self showHUD];
    [_homeVM requestBrandList:^{
        NSLog(@"结束");
        [self hideHUD];
        [self.tableView reloadData];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showHUD) name:@"SHOW_PROGRESS" object:nil];
    __weak typeof(self) weakSelf = self;
    _homeVM.resultBlock = ^{
        [weakSelf.tableView reloadData];
        [weakSelf hideHUD];
    };
    
    NSLog(@"%lf--%lf",self.tableView.y, self.tableView.height);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkAvailableAction) name:@"NETWORK_AVAILABLE" object:nil];
    
    
}

- (void)netWorkAvailableAction {

    if (_homeVM.firstBrandList.count == 0 || _homeVM.secondBrandList.count == 0 ||_homeVM.thirdBrandList.count == 0) {
        [_homeVM requestBrandList:^{
            NSLog(@"结束");
            [self.tableView reloadData];
        }];
    }
    
    
}


- (UIView *)bottomView {
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_WIDTH, 70)];
        UILabel *remindLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, K_WIDTH - 20, 20)];
        remindLabel.text = @"特别提醒:";
        remindLabel.textColor = [UIColor colorWithHexString:@"#007ede"];
        remindLabel.font = [UIFont systemFontOfSize:12];
        [_bottomView addSubview:remindLabel];
        
        UILabel *remingContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, K_WIDTH - 40, 40)];
        remingContentLabel.numberOfLines = 0;
        remingContentLabel.textColor = [UIColor whiteColor];
        remingContentLabel.text = @"为保障您的合法权益，请索要实物质保卡，并在线完成电子质保。";
        remingContentLabel.font = [UIFont systemFontOfSize:12];
        [_bottomView addSubview:remingContentLabel];
    }
    return _bottomView;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.contentInset = UIEdgeInsetsMake(kTopMargin, 0, 0, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView addSubview:self.topView];
        
    }
    return _tableView;
}

- (QuotationTopView *)topView {
    if (_topView == nil) {
        //tableView顶部
        CGFloat y = 90;
        if (KIsiPhoneX) {
            y = 60;
        }
        _topView = [[QuotationTopView alloc] initWithFrame:CGRectMake(0, -kTopMargin + y, K_WIDTH, kTopMargin - y)];
        __weak typeof(self) weakSelf = self;
        _topView.finishedBlock = ^(NSInteger index) {
            weakSelf.index = index;
            [weakSelf.tableView reloadData];
        };
    }
    return _topView;
}

#pragma mark -- UITableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_index == 0) {
        return self.homeVM.firstModel.rowCount;
    }else if (_index == 1) {
        return self.homeVM.secondModel.rowCount;
    }else {
        return self.homeVM.thirdModel.rowCount;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_index == 0) {
        if (indexPath.row == 0) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.firstModel.carBrandName;
            return cell;
        }else if (indexPath.row == 1) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.firstModel.carSeriesName;
            return cell;
        }else if (indexPath.row == 2) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.firstModel.carProductName;
            return cell;
        }else if (indexPath.row == 3) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Money];
            cell.priceLabel.text = [NSString stringWithFormat:@"整车价: %@元", self.homeVM.firstModel.price];
            return cell;
        }else if (indexPath.row == 4) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_TextDetail];
            cell.detalText.text = @"根据您选择的车型，如因特殊原因需要更换局部的情况，请参照以下比例付费。";
            [cell.detalText sizeToFit];
            return cell;
        }else if (indexPath.row == 5) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_ImgDetail];
            cell.detailImgView.image = [UIImage imageNamed:@"price_001"];
            return cell;
        }
    }else if (_index == 1) {
        if (indexPath.row == 0) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.secondModel.carBrandName;
            return cell;
        }else if (indexPath.row == 1) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.secondModel.carSeriesName;
            return cell;
        }else if (indexPath.row == 2) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.secondModel.carProductName;
            return cell;
        }else if (indexPath.row == 3) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Money];
            cell.priceLabel.text = [NSString stringWithFormat:@"整车价: %@元", self.homeVM.secondModel.price];
            return cell;
        }else if (indexPath.row == 4) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_TextDetail];
            cell.detalText.text = @"根据您选择的车型，如因特殊原因需要更换局部的情况，请参照以下比例付费。";
            [cell.detalText sizeToFit];
            return cell;
        }else if (indexPath.row == 5) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_ImgDetail];
            cell.detailImgView.image = [UIImage imageNamed:@"price_001"];
            return cell;
        }
    }else {
        if (indexPath.row == 0) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.thirdModel.carBrandName;
            return cell;
        }else if (indexPath.row == 1) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.thirdModel.carSeriesName;
            return cell;
        }else if (indexPath.row == 2) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.thirdModel.glaPartName;
            return cell;
        }else if (indexPath.row == 3) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
            cell.checkLabel.text = self.homeVM.thirdModel.glaProductName;
            return cell;
        }else if (indexPath.row == 4) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Money];
            cell.priceLabel.text = [NSString stringWithFormat:@"%@元", self.homeVM.thirdModel.price];
            return cell;
        }else if (indexPath.row == 5) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_TextDetail];
            cell.detalText.text = @"局部价格计算方式：\n全车套餐不包含天窗，以下为局部价格计算方式，以侧后价格为100%计算，（例如：普通天窗价格=该车型侧后报价 * 1/6）。";
            [cell.detalText sizeToFit];
            return cell;
        }else if (indexPath.row == 6) {
            HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_ImgDetail];
            cell.detailImgView.image = [UIImage imageNamed:@"price_002"];
            return cell;
        }
    }
    
    //空的cell
    HomeCell *cell = [HomeCell homeCellWithTableView:tableView andReuseIdentifier:HomeCellID_Check];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_index == 0 || _index == 1) {
        if (indexPath.row < 4) {
            return 50;
        }else if (indexPath.row == 4) {
            return [self.homeVM detailHeightWithIndex:self.index];
        }else {
            return (K_WIDTH - 30) / 5.75;
        }
    }else {
        if (indexPath.row < 5) {
            return 50;
        }else if (indexPath.row == 5) {
            return [self.homeVM detailHeightWithIndex:self.index];
        }else {
            return (K_WIDTH - 30) / 5.75;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.bottomView.bounds.size.height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return self.bottomView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld", indexPath.row);
    __weak typeof(self) weakSelf = self;
    if (_index == 0) {
        switch (indexPath.row) {
            case 0:
            {
                if (self.homeVM.firstBrandList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.firstBrandList];
                    
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        CarModel *model = data[row];
                        return model.cat_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        CarModel *model = data;
                        weakSelf.homeVM.firstModel.carBrandModel = model;
                        [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
                        [weakSelf.homeVM requestFirstSeriesList:^{
                            [weakSelf.tableView reloadData];
                            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
                            [weakSelf.homeVM requestPrice:self.index];
                        }];
                        
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }
            }
                break;
            case 1:
            {
                if (self.homeVM.firstSeriesList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.firstSeriesList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        CarModel *model = data[row];
                        return model.cat_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        CarModel *model = data;
                        weakSelf.homeVM.firstModel.carSeriesModel = model;
                        [weakSelf.tableView reloadData];
                        [weakSelf.homeVM requestPrice:self.index];
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }else {
                    [MBProgressHUD showError:@"请选择汽车品牌" toView:self.view];
                }
            }
                break;
            case 2:
            {
                if (self.homeVM.firstProList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.firstProList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        ProjectModel *model = data[row];
                        return model.pro_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        ProjectModel *model = data;
                        weakSelf.homeVM.firstModel.carProductModel = model;
                        [weakSelf.tableView reloadData];
                        [weakSelf.homeVM requestPrice:self.index];
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }
            }
                break;
            default:
                break;
        }
    }else if (_index == 1) {
        switch (indexPath.row) {
            case 0:
            {
                if (self.homeVM.secondBrandList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.secondBrandList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        CarModel *model = data[row];
                        return model.cat_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        CarModel *model = data;
                        weakSelf.homeVM.secondModel.carBrandModel = model;
                        [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
                        [weakSelf.homeVM requestSecondSeriesList:^{
                            [weakSelf.tableView reloadData];
                            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
                            [weakSelf.homeVM requestPrice:self.index];
                        }];
                        
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }
            }
                break;
            case 1:
            {
                if (self.homeVM.secondSeriesList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.secondSeriesList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        CarModel *model = data[row];
                        return model.cat_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        CarModel *model = data;
                        weakSelf.homeVM.secondModel.carSeriesModel = model;
                        [weakSelf.tableView reloadData];
                        [weakSelf.homeVM requestPrice:self.index];
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }else {
                    [MBProgressHUD showError:@"请选择汽车品牌" toView:self.view];
                }
            }
                break;
            case 2:
            {
                if (self.homeVM.secondProList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.secondProList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        ProjectModel *model = data[row];
                        return model.pro_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        ProjectModel *model = data;
                        weakSelf.homeVM.secondModel.carProductModel = model;
                        [weakSelf.tableView reloadData];
                        [weakSelf.homeVM requestPrice:self.index];
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }
            }
                break;
            default:
                break;
        }
    }else if (_index == 2) {
        switch (indexPath.row) {
            case 0:
            {
                if (self.homeVM.thirdBrandList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.thirdBrandList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        CarModel *model = data[row];
                        return model.cat_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        CarModel *model = data;
                        weakSelf.homeVM.thirdModel.carBrandModel = model;
                        [MBProgressHUD showHUDAddedTo:weakSelf.view animated:YES];
                        [weakSelf.homeVM requestThirdSeriesList:^{
                            [weakSelf.tableView reloadData];
                            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
                            [weakSelf.homeVM requestPrice:self.index];
                        }];
                        
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }
            }
                break;
            case 1:
            {
                if (self.homeVM.thirdSeriesList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.thirdSeriesList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        CarModel *model = data[row];
                        return model.cat_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        CarModel *model = data;
                        weakSelf.homeVM.thirdModel.carSeriesModel = model;
                        [weakSelf.tableView reloadData];
                        [weakSelf.homeVM requestPrice:self.index];
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }else {
                    [MBProgressHUD showError:@"请选择汽车品牌" toView:self.view];
                }
            }
                break;
            case 2:
            {
                if (self.homeVM.thirdPartList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.thirdPartList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        ProjectModel *model = data[row];
                        return model.gla_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        ProjectModel *model = data;
                        weakSelf.homeVM.thirdModel.glaPartModel = model;
                        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                        [weakSelf.homeVM requestThirdProjectList:^{
                            [weakSelf.tableView reloadData];
                            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
                            [weakSelf.homeVM requestPrice:self.index];
                        }];
                        
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }
            }
                break;
            case 3:
            {
                if (self.homeVM.thirdProList.count > 0) {
                    PickerViewController *picker = [[PickerViewController alloc] initWithDataSource:self.homeVM.thirdProList];
                    picker.titleForRowBlock = ^NSString *(id data, NSInteger row) {
                        ProjectModel *model = data[row];
                        return model.gla_name;
                    };
                    picker.confirmBlock = ^(id data) {
                        ProjectModel *model = data;
                        weakSelf.homeVM.thirdModel.glaProductModel = model;
                        [weakSelf.tableView reloadData];
                        [weakSelf.homeVM requestPrice:self.index];
                    };
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self presentViewController:picker animated:YES completion:nil];
                    });
                }
            }
                break;
            default:
                break;
        }
    }
    
}











@end
